package com.jinpeng.rabbitmq;


import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


/**
 * @Todo消息发送控制器
 * @author JINPENG
 */
@RestController
public class MsgController {
    /**
     * 注入rabbitTemplate，用以发送与接收消息
     */
    @Autowired
    RabbitTemplate rabbitTemplate;
    /**
     * 绑定键
     */
    public final static String CAR = "topic.car";
    public final static String HOUSE = "topic.house";

    /**
     * Direct exchange（直连交换机）
     * 发送主题交换机类型的消息(绑定路由key 为 topic.man)
     * @return  String
     */
    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage() {
        for (int i = 0; i <100 ; i++) {
            String messageId = String.valueOf(UUID.randomUUID());
            String messageData = "hello, this is  DirectMessage!"+i;
            Map<String, Object> map = getStringObjectMap(messageId, messageData);
            //将消息携带绑定键值：directRouting 发送到交换机directExchange
            rabbitTemplate.convertAndSend("directExchange", "directRouting", map);
        }

        return "ok";
    }



    /**
     * Topic exchange（主题交换机）
     * 发送主题交换机类型的消息(绑定路由key 为 topic.man)
     * @return  String
     */
    @GetMapping("/sendTopicMessageCar")
    public String sendTopicMessageCar() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "hello, this is  CAR  topicMessage !";
        Map<String, Object> carMap = getStringObjectMap(messageId, messageData);
        rabbitTemplate.convertAndSend("topicExchange", CAR, carMap);
        return "ok";
    }
    /**
     * Topic exchange（主题交换机）
     * 发送主题交换机类型的消息(绑定路由key 为 topic.woman)
     * @return  String
     */
    @GetMapping("/sendTopicMessageHouse")
    public String sendTopicMessageHouse() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "hello, this is  HOUSE  topicMessage ! ";
        Map<String, Object> houseMap = getStringObjectMap(messageId, messageData);
        rabbitTemplate.convertAndSend("topicExchange", HOUSE, houseMap);
        return "ok";
    }

    /**
     * Fanout exchange（扇型交换机）
     * 发送扇型交换机类型的消息(无需绑定路由键)
     * @return  String
     */
    @GetMapping("/sendFanoutMessage")
    public String sendFanoutMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "hello, this is  FanoutMessage ";
        Map<String, Object> map = getStringObjectMap(messageId, messageData);
        rabbitTemplate.convertAndSend("fanoutExchange", null, map);
        return "ok";
    }



    /**
     * 消息推送到server，但是在server里找不到交换机
     * @return String
     */
    @GetMapping("/NoExchangeMessageAck")
    public String NoExchangeMessageAck() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: non-existent-exchange test message ";
        Map<String, Object> map = getStringObjectMap(messageId, messageData);
        rabbitTemplate.convertAndSend("non-existent-exchange", "TestDirectRouting", map);
        return "ok";
    }

    /**
     * 消息推送到server，找到交换机但是没有对应的队列
     * @return String
     */
    @GetMapping("/NoQueryMessageAck")
    public String NoQueryMessageAck() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: lonelyDirectExchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("topicExchange", "TestDirectRouting", map);
        return "ok";
    }

    /**
     * 消息创建方法
     * @param messageId
     * @param messageData
     * @return
     */
    private Map<String, Object> getStringObjectMap(String messageId, String messageData) {
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        return map;
    }

}
